const express = require("express");
const router = express.Router();
const {asyncHandler} =require("../middleWare/getSentResult");
const Blog = require("../server/blogServer"); 

const blogType = require("../server/blogTypeServer");

router.get("/list/:type",asyncHandler(async(req,res,next)=>{
    const params = req.params;
    const query = req.query;
    const htmlContent = query._id || 0;
    const toch = query.toch || 0;
    const limit = query.limit || 10;
    const skip = query.skip || 1;
    const filters = {};
    if(params.type != "全部"){
        filters.blogType = params.type
    }
    if(query._id){
        filters._id = query._id;
    }
    try{
        const result = await Blog.getBlogList(req.originalUrl,filters,htmlContent,toch,limit,skip);
        return result;
    }catch(e){
        return e;
    }
   
}))

router.get("/:id",asyncHandler (async (req,res,next)=>{
   try{
    const result = await Blog.getBlogDetalis(req.params.id);
    return result;

   } catch(e){
       return e
   }
   
}) );


router.post("/addType",asyncHandler(async (req,res,next)=>{
    
    if(!req.body.typeString) return "请在Body中添加typeString" 
    const typeString = req.body.typeString;
    try{

        const result = await blogType.addType(typeString);
        return result;

    }catch(e){
        return 'err'+ e.name
    }
    
}));




























module.exports = router;